package com.qianyucc.blog.controller.admin;

import com.qianyucc.blog.model.dto.*;
import com.qianyucc.blog.model.vo.*;
import com.qianyucc.blog.service.*;
import io.swagger.annotations.*;
import lombok.extern.slf4j.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.validation.annotation.*;
import org.springframework.web.bind.annotation.*;

import static com.qianyucc.blog.global.Constants.*;

/**
 * @author lijing
 * @date 2019-10-18 20:16
 * @description 管理博客文章
 */
@RestController("adminArticleController")
@RequestMapping("admin/articles")
@Slf4j
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    @ApiOperation("插入文章")
    @PostMapping("/")
    public ResultVO insArticle(@RequestBody @Validated ArticleDTO articleDTO) {
        articleService.insArticle(articleDTO);
        return ResultVO.ok("插入文章成功！");
    }

    @ApiOperation("修改文章")
    @PutMapping("/")
    public ResultVO updArticle(@RequestBody @Validated ArticleDTO articleDTO) {
        articleService.updArticle(articleDTO);
        return ResultVO.ok("更新文章成功！");
    }

    @ApiOperation("根据对应文章id删除文章")
    @DeleteMapping("/{id}")
    public ResultVO delArticleById(@PathVariable String id) {
        articleService.delete(id);
        return ResultVO.ok("ID为" + id + "的文章删除成功！");
    }

    @ApiOperation("分页获取文章列表")
    @GetMapping("/")
    public PageInfoVO<SimpleArticleVO> getPageArticles(@RequestParam(value = "pageNumber", defaultValue = "1") Integer pageNumber) {
        PageInfoVO<SimpleArticleVO> pageInfoVO = articleService.findByCondition(pageNumber, ADMIN_PAGE_SIZE, null, null);
        return pageInfoVO;
    }

    @ApiOperation("根据查询条件筛选文章")
    @PostMapping("/query")
    public PageInfoVO<SimpleArticleVO> searchArticles(@RequestBody QueryConditionDTO condition) {
        PageInfoVO<SimpleArticleVO> pageInfoVO = articleService.findArticles(condition);
        return pageInfoVO;
    }
}